package com.imyours.webservice.services;

import java.util.Date;

import org.hibernate.classic.Session;

import com.imyours.model.announcement.Announcement;
import com.imyours.model.user.UserEntity;
import com.imyours.model.util.HibernateUtil;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

/**发布公告的接口
 * @author SLevenc
 *
 */
@Service(name = "publishAnnouncement")
public class PublishAnnouncement extends ContextService {

	public PublishAnnouncement(Context context) {
		super(context);
	}

	public void execute() {
		if (isAdmin()) {
			UserEntity user = getUserEntity();
			String content = context.getParameter().get("content");
			if (content != null) {
				Announcement a = new Announcement();
				a.setContent(content);
				a.setDate(new Date());
				a.setSender(user);
				Session se = HibernateUtil.getSessionFactory().openSession();
				se.beginTransaction();
				se.save(a);
				se.getTransaction().commit();
				se.close();
				context.success();
			}
		}
	}
}
